package myactionplugin;

import java.awt.event.ActionEvent;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.swing.JOptionPane;


import com.nomagic.magicdraw.actions.MDAction;
import com.nomagic.magicdraw.core.Application;
import com.nomagic.magicdraw.ui.dialogs.MDDialogParentProvider;
import com.nomagic.uml2.ext.magicdraw.auxiliaryconstructs.mdmodels.Model;
import com.nomagic.uml2.ext.magicdraw.classes.mdinterfaces.Interface;
import com.nomagic.uml2.ext.magicdraw.classes.mdkernel.Class;
import com.nomagic.uml2.ext.magicdraw.classes.mdkernel.Element;
import com.nomagic.uml2.ext.magicdraw.classes.mdkernel.Operation;
import com.nomagic.uml2.ext.magicdraw.classes.mdkernel.Property;
import com.nomagic.uml2.ext.magicdraw.classes.mdkernel.VisibilityKind;
import com.nomagic.uml2.ext.magicdraw.mdprofiles.Stereotype;
import com.nomagic.uml2.ext.magicdraw.classes.*;

import freemarker.template.Template;

/**
 *
* <p>Title:SimpleAction </p>
* <p>Description: 定义了点击菜单的行为 </p>
* <p>Company: </p> 
* @author 二胡
* @date 2018-7-17上午10:24:50
 */
public class SimpleAction  extends MDAction{
	 
		public SimpleAction(String id, String name){
			super(id, name, null, null);
	    }

	    /**
	     * 插件运行的核心方法
	     */
	    public void actionPerformed(ActionEvent e){
	    	
	    	//获取当前工程模型
	    	Model model = Application.getInstance().getProject().getModel();
	    	//用来存储当前project下所有的元素
	    	List<Element> children = new ArrayList();
	    	if(model.hasOwnedElement()){
	    		//如果有子元素
	    		for(Iterator it = model.getOwnedElement().iterator();it.hasNext();){
	    			Element ownedElement = (Element) it.next();
	    			String humaname= ownedElement.getHumanName();
	    			if(humaname.startsWith("Package") && !humaname.equals("Package UML Standard Profile")){
	    				//以Package开头
	    				children.add(ownedElement);
	    				for (int i = 0; i < children.size(); i++){
	    					//循环遍历子元素并存储到集合中
	    					ownedElement = (Element) children.get(i);
	    					children.addAll(ownedElement.getOwnedElement());
	    				}
	    			}
	    		}
	    	}
	    	
	    	//删除集合中不需要的元素
	    	for(int i=0;i<children.size();i++){
	    		if(children.get(i).getHumanType().equals("Instance Specification") || children.get(i).getHumanType().startsWith("Instance") || children.get(i).getHumanType().equals("Parameter") || children.get(i).getHumanType().startsWith("Parameter")){
	    			children.remove(i);
	    		}
	    	}
	    	int enumIndex=0;
	    	//拼接字符串
	    	String a = "";
	    	
	    	
	    	for(Element element : children){
	    	
	    		String TdlStereoType = null;
	    		//获取每一个元素的Stereotype
	    		if(!element.getHumanType().equals("Property") && !element.getHumanType().equals("Enumeration Literal")&& !element.getHumanType().equals("Operation") && !element.getHumanType().equals("Parameter")){
	    			List<Stereotype> list = TDLStereotypeHelper.getTDLStereotypeName(element);
	    			for(int i=0;i<list.size();i++){
	    				TdlStereoType = list.get(0).getName();
	    			}
	    			
	    			//创建包代码
	    			if(TdlStereoType.equals("TENA::Package")){
		    			if(a==""){
		    				a = a+element.getHumanName()+"{"+"\r\n"+"}";
		    						    			}else{
		    				String a1 = a.substring(0, a.length()-1);
		    				a = a1+element.getHumanName()+"{"+"\r\n"+"}"+"\r\n"+"}";
		    			}
		    			
		    		}else if(TdlStereoType.equals("TENA::Class")){
		    			
		    			
		    			//对<<TENA：：Class>>判断
		    			String a1 = a.substring(0, a.length()-1);
		    			String[] split = element.getHumanName().split(" ");
		    			String name = split[1];
		    			
		    			a = a1+" class "+name+"{"+"\r\n"+" }"+"\r\n"+"}";
		    			int classNameIndex = a.indexOf(name);
		    			
		    			
		    			//遍历属性
		    			List<Property> attributes = ((Class)element).getOwnedAttribute();
		    			//遍历方法
	    				List<Operation> operations = ((Class)element).getOwnedOperation();
		    			//如果有属性
		    			if(attributes.size()>0){
		    				//获得第一个属性名
		    				String firstProperty = attributes.get(0).getName();
		    				for(Property property : attributes){
		    					String propertyName = property.getName();//属性名
		    					String dataType = property.getType().getName();//获取返回值类型
		    					VisibilityKind visibility = property.getVisibility();//获取约束类型
		    					
		    					
		    					int start= a.indexOf(name);
		    					int end = start+name.length()+1;
		    					StringBuffer buffer = new StringBuffer(a);
		    					buffer.insert(end,"\r\n"+"     "+visibility+" "+dataType+" "+propertyName+";");
		    					a = buffer.toString();
		    				}
		    				//如果有方法
		    				if(operations.size()>0){
		    					for(Operation operation : operations){
			    					String operationName = operation.getName();//方法名
			    					String dataType = operation.getType().getName();//获取返回值类型
			    					VisibilityKind visibility = operation.getVisibility();//获取约束类型
			    					
			    					
			    					int start= a.indexOf(firstProperty,classNameIndex);
			    					int end = start+firstProperty.length()+1;
			    					StringBuffer buffer = new StringBuffer(a);
			    					buffer.insert(end,"\r\n"+"     "+visibility+" "+dataType+" "+operationName+"(){}");
			    					a = buffer.toString();
			    				}
		    				}
		    				
		    				
		    				
		    			}else{
		    				//如果没有属性
		    				if(operations.size()>0){
		    					for(Operation operation : operations){
			    					String operationName = operation.getName();//方法名
			    					String dataType = operation.getType().getName();//获取返回值类型
			    					VisibilityKind visibility = operation.getVisibility();//获取约束类型
			    					int start= a.indexOf(name);
			    					int end = start+name.length()+1;
			    					StringBuffer buffer = new StringBuffer(a);
			    					buffer.insert(end,"\r\n"+"     "+visibility+" "+dataType+" "+operationName+"(){};");
			    					a = buffer.toString();
			    				}
		    				}
		    			}
		    			
		    			
		    		}else if(TdlStereoType.equals("TENA::Exception")){
		    			
		    			
		    			
		    			//对<<TENA：：Exception>>判断
		    			String a1 = a.substring(0, a.length()-1);
		    			String[] split = element.getHumanName().split(" ");
		    			String name = split[1];
		    			a = a1+" exception "+name+"{"+"\r\n"+" }"+"\r\n"+"}";
		    			int classNameIndex = a.indexOf(name);
		    			
		    			//遍历属性
		    			List<Property> attributes = ((Class)element).getOwnedAttribute();
		    			//遍历方法
	    				List<Operation> operations = ((Class)element).getOwnedOperation();
		    			//如果有属性
		    			if(attributes.size()>0){
		    				//获得第一个属性名
		    				String firstProperty = attributes.get(0).getName();
		    				for(Property property : attributes){
		    					String propertyName = property.getName();//属性名
		    					String dataType = property.getType().getName();//获取返回值类型
		    					VisibilityKind visibility = property.getVisibility();//获取约束类型
		    					
		    					
		    					int start= a.indexOf(name);
		    					int end = start+name.length()+1;
		    					StringBuffer buffer = new StringBuffer(a);
		    					buffer.insert(end,"\r\n"+"     "+visibility+" "+dataType+" "+propertyName+";");
		    					a = buffer.toString();
		    				}
		    				//如果有方法
		    				if(operations.size()>0){
		    					for(Operation operation : operations){
			    					String operationName = operation.getName();//方法名
			    					String dataType = operation.getType().getName();//获取返回值类型
			    					VisibilityKind visibility = operation.getVisibility();//获取约束类型
			    					
			    					
			    					int start= a.indexOf(firstProperty,classNameIndex);
			    					int end = start+firstProperty.length()+1;
			    					StringBuffer buffer = new StringBuffer(a);
			    					buffer.insert(end,"\r\n"+"     "+visibility+" "+dataType+" "+operationName+"(){}");
			    					a = buffer.toString();
			    				}
		    				}
		    				
		    				
		    			}else{
		    				//如果没有属性
		    				if(operations.size()>0){
		    					for(Operation operation : operations){
			    					String operationName = operation.getName();//方法名
			    					String dataType = operation.getType().getName();//获取返回值类型
			    					VisibilityKind visibility = operation.getVisibility();//获取约束类型
			    					int start= a.indexOf(name);
			    					int end = start+name.length()+1;
			    					StringBuffer buffer = new StringBuffer(a);
			    					buffer.insert(end,"\r\n"+"     "+visibility+" "+dataType+" "+operationName+"(){};");
			    					a = buffer.toString();
			    				}
		    				}
		    			}
		    			
		    			}else if(TdlStereoType.equals("TENA::Message")){
		    				
		    				
		    				
		    				//对<<TENA：：Message>>判断
		    				String a1 = a.substring(0, a.length()-1);
			    			String[] split = element.getHumanName().split(" ");
			    			String name = split[1];
			    			a = a1+" message "+name+"{"+"\r\n"+" }"+"\r\n"+"}";
			    			int classNameIndex = a.indexOf(name);
			    			
			    			
			    			
			    			//遍历属性
			    			List<Property> attributes = ((Class)element).getOwnedAttribute();
			    			//遍历方法
		    				List<Operation> operations = ((Class)element).getOwnedOperation();
			    			//如果有属性
			    			if(attributes.size()>0){
			    				//获得第一个属性名
			    				String firstProperty = attributes.get(0).getName();
			    				for(Property property : attributes){
			    					String propertyName = property.getName();//属性名
			    					String dataType = property.getType().getName();//获取返回值类型
			    					VisibilityKind visibility = property.getVisibility();//获取约束类型
			    					
			    					
			    					int start= a.indexOf(name);
			    					int end = start+name.length()+1;
			    					StringBuffer buffer = new StringBuffer(a);
			    					buffer.insert(end,"\r\n"+"     "+visibility+" "+dataType+" "+propertyName+";");
			    					a = buffer.toString();
			    				}
			    				//如果有方法
			    				if(operations.size()>0){
			    					for(Operation operation : operations){
				    					String operationName = operation.getName();//方法名
				    					String dataType = operation.getType().getName();//获取返回值类型
				    					VisibilityKind visibility = operation.getVisibility();//获取约束类型
				    					
				    					
				    					int start= a.indexOf(firstProperty,classNameIndex);
				    					int end = start+firstProperty.length()+1;
				    					StringBuffer buffer = new StringBuffer(a);
				    					buffer.insert(end,"\r\n"+"     "+visibility+" "+dataType+" "+operationName+"(){}");
				    					a = buffer.toString();
				    				}
			    				}
			    				
			    				
			    			}else{
			    				//如果没有属性
			    				if(operations.size()>0){
			    					for(Operation operation : operations){
				    					String operationName = operation.getName();//方法名
				    					String dataType = operation.getType().getName();//获取返回值类型
				    					VisibilityKind visibility = operation.getVisibility();//获取约束类型
				    					int start= a.indexOf(name);
				    					int end = start+name.length()+1;
				    					StringBuffer buffer = new StringBuffer(a);
				    					buffer.insert(end,"\r\n"+"     "+visibility+" "+dataType+" "+operationName+"(){};");
				    					a = buffer.toString();
				    				}
			    				}
			    			}
		    			}else if(TdlStereoType.equals("TENA::LoaclClass")){
		    				//对<<TENA：：LoaclClass>>判断
		    				String a1 = a.substring(0, a.length()-1);
			    			String[] split = element.getHumanName().split(" ");
			    			String name = split[1];
			    			a = a1+" local class "+name+"{"+"\r\n"+" }"+"\r\n"+"}";
			    			int classNameIndex = a.indexOf(name);
			    			
			    			
			    			//遍历属性
			    			List<Property> attributes = ((Class)element).getOwnedAttribute();
			    			//遍历方法
		    				List<Operation> operations = ((Class)element).getOwnedOperation();
			    			//如果有属性
			    			if(attributes.size()>0){
			    				//获得第一个属性名
			    				String firstProperty = attributes.get(0).getName();
			    				for(Property property : attributes){
			    					String propertyName = property.getName();//属性名
			    					String dataType = property.getType().getName();//获取返回值类型
			    					VisibilityKind visibility = property.getVisibility();//获取约束类型
			    					
			    					
			    					int start= a.indexOf(name);
			    					int end = start+name.length()+1;
			    					StringBuffer buffer = new StringBuffer(a);
			    					buffer.insert(end,"\r\n"+"     "+visibility+" "+dataType+" "+propertyName+";");
			    					a = buffer.toString();
			    				}
			    				//如果有方法
			    				if(operations.size()>0){
			    					for(Operation operation : operations){
				    					String operationName = operation.getName();//方法名
				    					String dataType = operation.getType().getName();//获取返回值类型
				    					VisibilityKind visibility = operation.getVisibility();//获取约束类型
				    					
				    					
				    					int start= a.indexOf(firstProperty,classNameIndex);
				    					int end = start+firstProperty.length()+1;
				    					StringBuffer buffer = new StringBuffer(a);
				    					buffer.insert(end,"\r\n"+"     "+visibility+" "+dataType+" "+operationName+"(){}");
				    					a = buffer.toString();
				    				}
			    				}
			    				
			    				
			    			}else{
			    				//如果没有属性
			    				if(operations.size()>0){
			    					for(Operation operation : operations){
				    					String operationName = operation.getName();//方法名
				    					String dataType = operation.getType().getName();//获取返回值类型
				    					VisibilityKind visibility = operation.getVisibility();//获取约束类型
				    					int start= a.indexOf(name);
				    					int end = start+name.length()+1;
				    					StringBuffer buffer = new StringBuffer(a);
				    					buffer.insert(end,"\r\n"+"     "+visibility+" "+dataType+" "+operationName+"(){};");
				    					a = buffer.toString();
				    				}
			    				}
			    			}
		    			}else if(TdlStereoType.equals("TENA::Enumeration")){
		    				//对<<TENA：：Enumeration>>判断
		    				String a1 = a.substring(0, a.length()-1);
			    			String[] split = element.getHumanName().split(" ");
			    			String name = split[1];
			    			
			    			
			    			a = a1+" enumeration "+name+"{"+"\r\n"+" }"+"\r\n"+"}";
		    			}else if(TdlStereoType.equals("TENA::Interface")){
		    				//对<<TENA：：Interface>>判断
		    				String a1 = a.substring(0, a.length()-1);
			    			String[] split = element.getHumanName().split(" ");
			    			String name = split[1];
			    			a = a1+" interface "+name+"{"+"\r\n"+" }"+"\r\n"+"}";
			    			
			    			
			    			//遍历方法
			    			List<Operation> operations = ((Interface)element).getOwnedOperation();
			    			if(operations.size()>0){
			    				for(Operation operation : operations){
				    				String operationName = operation.getName();//方法名
				    				String dataType = operation.getType().getName();//获取返回值类型
				    				VisibilityKind visibility = operation.getVisibility();//获取约束类型
				    				
				    				
				    				int start= a.indexOf(name);
					    			int end = start+name.length()+1;
					    			StringBuffer buffer = new StringBuffer(a);
					    			buffer.insert(end,"\r\n"+"     "+visibility+" "+dataType+" "+operationName+"(){}");
					    			a = buffer.toString();
				    			}
			    			}
			    			
			    			
		    			}
	    		}else{
		    		if(element.getHumanType().equals("Enumeration Literal")){
		    				//对枚举属性判断
		    				//得到父元素
			    			Element owner = element.getOwner();
			    			String[] split = owner.getHumanName().split(" ");
			    			//得到父元素的类名
			    			String ownerName = split[1];
			    			int start= a.indexOf(ownerName);
			    			int end = start+ownerName.length()+1;
			    			//得到属性名
			    			String pName = element.getHumanName().split(" ")[2];
			    			
			    			
			    			StringBuffer buffer = new StringBuffer(a);
			    			if(enumIndex==0){
			    				buffer.insert(end,"\r\n"+"     "+pName);
			    			}else{
			    				buffer.insert(end,"\r\n"+"     "+pName+",");
			    			}
			    			a = buffer.toString();
			    			//判断Enumeration Literal第几次进来
			    			enumIndex++;
		    			  }
	    		}
	    		
	    		
	    		//输出字符串到文件中
	    		File fp=new File("d:\\"+"tdlCode.txt");
	    		PrintWriter pfp;
	    		try {
	    			pfp = new PrintWriter(fp);
	    			pfp.print(a);
	    			pfp.close();
	    			
	    		} catch (FileNotFoundException e1) {
	    			e1.printStackTrace();
	    		}
	    		
	    	}
	    	
	    	//转java代码
	    	ChangeToJava.changeToJava(a);
	    	
	    	JOptionPane.showMessageDialog(MDDialogParentProvider.getProvider().getDialogParent(),"tdl代码已生成  d:\\tdlCode.txt");
	    	
	    	//转java代码
	    	/*String packageName = ToJavaUtils.getPackage(a);
	    	File fp=new File("d:\\"+packageName);
	    	fp.mkdir();
	    	a = ToJavaUtils.getMainString(a);
	    	String[] list = ToJavaUtils.changeJava(a);
	    	for(String string : list){
	    		String className = ToJavaUtils.getClassName(string);
	    		File fp1=new File("d:\\"+packageName+"\\"+className+".java");
	    		PrintWriter pfp;
	    		try {
	    			pfp = new PrintWriter(fp1);
	    			pfp.print("package "+packageName+";\r\n"+"public"+string+"}");
	    			pfp.close();
	    			
	    		} catch (FileNotFoundException e1) {
	    			e1.printStackTrace();
	    		}
	    	}
	    	JOptionPane.showMessageDialog(MDDialogParentProvider.getProvider().getDialogParent(),"java代码已生成  d:\\"+packageName);*/
	    }
}
